<%@ page import="java.util.ArrayList" %>
<%@ page import="java.sql.*" %>
<%@ page import="bean.Hotel" %>
<%@ page import="bean.Login" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="java.util.Date" %>
<%--
  Created by IntelliJ IDEA.
  User: ASUS
  Date: 2022/7/3
  Time: 18:08
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<jsp:useBean id="rooms" class="bean.Rooms" scope="session"/>
<html>
<head>
    <title>酒店详情</title>
    <!-- 引入 Bootstrap -->
    <link href="css/BootStrap.css" rel="stylesheet">
    <script src="js/BootStrap.js"></script>
    <link href="css/bootstrap.min.css" rel="stylesheet">
    <link href="css/styles.css" rel="stylesheet">
    <link href="css/bootstrap.min.css" rel="stylesheet">
    <link href="css/animate.min.css" rel="stylesheet">

    <style>
        .room{
            width: 310px;
            margin-left: 10px;
        }
        .fl_left{
            float: left;
        }
        .bed{
            width: 120px;
            margin-left: 30px;
        }
        .price{
            width: 120px;
            margin-left: 34px;
            text-align: right;
        }
        .fl_right{
            float: right;
        }

    </style>
</head>

<body id="home">
<header id="header">
    <nav id="main-nav" class="navbar navbar-default navbar-fixed-top" role="banner">
        <div class="navbar-header">
            <a class="navbar-brand" ><img src="image/图标.png" alt="logo"></a>
        </div>
        <div class="collapse navbar-collapse navbar-right">
            <ul class="nav navbar-nav">
                <li class="scroll"><a href="login.jsp">登录</a></li>
                <li class="scroll"><a href="register.jsp">注册</a></li>
                <li class="scroll"><a href="person.jsp">个人空间</a></li>
<%--                <li class="scroll"><a href="#">联系客服</a></li>--%>
            </ul>
        </div>
    </nav>
</header>
<br>
<br>
<div class="background" id="background">
    <br>
    <br>
    <div class="container">
        <div class="container-fluid">
<%!String hotelName;
    String hotelAddress;
%>
            <h2><%
                HttpSession session1 = request.getSession();
                Hotel hotelBean= (Hotel) session1.getAttribute("hotelBean");
                    hotelName=hotelBean.getHotelName();
                    out.print(hotelName);%></h2>
            <h3><%hotelAddress=hotelBean.getHotelAddress();
                out.print(hotelAddress);%></h3>

            <div class="row">
                <img src="image/index_1.jpg">
                <img src="image/index_1.jpg">
            </div>
            <br>
            <br>
        </div>
    </div>
    <div class="container">
        <div class="container-fluid">
            <ul class="nav nav-tabs">
                <li class="active"><a href="#">房型预订</a></li>
            </ul>
            <br>
            <div class="row">
                <div class="G_typeBox clearfix">
                    <span class="room fl_left">入住</span>
                    <span class="room fl_left">离店</span>
                </div>
            </div>
            <div class="row">
                <form action="QueryServlet">
                <span class="room fl_left"><input type="text" class="form-control" placeholder="请输入入住日期，如20220710" name="Checkin"/></span>
                <span class="room fl_left"><input type="text" class="form-control" placeholder="请输入离店日期，如20220711" name="Checkout"></span>
                <span class="price fl_right"><input type="submit" class="btn btn-info" value="确定"></span>
                </form>
        </div>
    </div>
    <br>
    <hr>
    <div class="container">
        <div class="container-fluid">
            <div class="row">
                <div class="G_typeBox clearfix">
                    <span class="room fl_left">房间名称</span>
                    <span class="room fl_left">房间大小</span>
                    <span class="bed fl_left">床型</span>
                    <span class="price fl_left">房价</span>
                </div>
            </div>
            <br>
            <%
                String TAG = "detail.jsp";
                Login loginBean=null;
                loginBean=(Login)session.getAttribute("loginBean");

                if (loginBean==null){
                    ArrayList<String> roomName=rooms.getRoomName();
                    ArrayList<String> roomID=rooms.getRoomId();
                    ArrayList<String> roomType = rooms.getRoomType();
                    ArrayList<String> roomSize = rooms.getRoomSize();
                    ArrayList<String> price = rooms.getPrice();
                    if (roomType.size()==0){
                        out.print("很抱歉，当前已无剩余房间！");
                    }else {
                        for (int i=0;i<roomType.size();i++){
                            out.print(" <div class=\"row\">");
                            out.print(" <div class=\"img_box\" tabindex=\"0\">");
                            out.print("<span class=\"room fl_left\">");
                            out.print(" <img alt=\"物理房型图片\" src=\"https://himg5.qunarzz.com/imgs/202109/11/C.RXfq77QLV33kx4ysQi240.jpg\" tabindex=\"0\">");
//                    out.print("<div class=\"words\" tabindex=\"0\"></div>");
                            out.print("<div class=\"roomInformList\" style=\"cursor: default;\" tabindex=\"0\">"+roomName.get(i)+"</div></span>");
                            out.print(" <span class=\"bed fl_left\">"+roomSize.get(i)+"平方米</span>");
                            out.print(" <span class=\"bed fl_left\">"+roomType.get(i)+"</span>");
                            out.print("<span class=\"price fl_left\" style=\"color: rgb(255,112,74)\">"+price.get(i)+"￥</span>");
                            out.print(" <span class=\"price fl_right\"><button type=\"button\" class=\"btn btn-info\"><a href='booking.jsp?RoomID=\""+roomID.get(i)+"\"'>预订</a></button></span>");
                            out.print(" </div>" + "</div>" + "<hr>");
                        }
                    }

                }else {
                    String UserID = loginBean.getLoginID();
                    System.out.println(TAG + UserID);
                    try {
                        //连接数据库
                        Class.forName("org.mariadb.jdbc.Driver");
                        Connection conn = DriverManager.getConnection("jdbc:mariadb://localhost:3306/hotel-management-system", "root", "123456");
                        System.out.println(TAG + "数据库连接成功！");
                        PreparedStatement stat;
                        ResultSet rs;
                        String time = new SimpleDateFormat("yyyyMMdd").format(new Date());
                        int checkin=Integer.valueOf(time).intValue();
                        String sql = "select * from orders where UserID='" + UserID + "'order by OrderDate desc";
                        stat = conn.prepareStatement(sql);
                        rs = stat.executeQuery();
                        int m = 0;
                        rs.beforeFirst();
                        if (!rs.next()) {
                            System.out.println("未查询到结果");
                        }

                        rs.beforeFirst();
                        while (rs.next()) {
                            String orderDate=rs.getString("OrderDate");
                            String orderYear=orderDate.substring(0,3);
                            String orderMonth=orderDate.substring(5,6);
                            String orderDay=orderDate.substring(8,9);
                            orderDate=(orderYear+orderMonth+orderDay);
                            int order=Integer.valueOf(orderDate).intValue();
                            if (order-checkin<=7){
                            if (rs.getString("OrderCondition").equals("已取消")) {
                                m++;
//                            } else if (rs.getString("OrderCondition").equals("成功预订")) {
//                                m=0;
//                            }
                            }}

                        }
                        System.out.println(m+"detail180行");
                        if (m >= 3) {
                            out.print("很抱歉，您已取消三次订单，7天之后将恢复预订权限。");
                        } else {
                            ArrayList<String> roomName=rooms.getRoomName();
                            ArrayList<String> roomID=rooms.getRoomId();
                            ArrayList<String> roomType = rooms.getRoomType();
                            ArrayList<String> roomSize = rooms.getRoomSize();
                            ArrayList<String> price = rooms.getPrice();
                            if (roomType.size() == 0) {
                                out.print("很抱歉，当前已无剩余房间！");
                            } else {
                                for (int i = 0; i < roomType.size(); i++) {
                                    out.print(" <div class=\"row\">");
                                    out.print(" <div class=\"img_box\" tabindex=\"0\">");
                                    out.print("<span class=\"room fl_left\">");
                                    out.print(" <img alt=\"物理房型图片\" src=\"https://himg5.qunarzz.com/imgs/202109/11/C.RXfq77QLV33kx4ysQi240.jpg\" tabindex=\"0\">");
//                    out.print("<div class=\"words\" tabindex=\"0\"></div>");
                                    out.print("<div class=\"roomInformList\" style=\"cursor: default;\" tabindex=\"0\">"+roomName.get(i)+"</div></span>");
                                    out.print(" <span class=\"bed fl_left\">"+roomSize.get(i)+"平方米</span>");
                                    out.print(" <span class=\"bed fl_left\">"+roomType.get(i)+"</span>");
                                    out.print("<span class=\"price fl_left\" style=\"color: rgb(255,112,74)\">"+price.get(i)+"￥</span>");
                                    out.print(" <span class=\"price fl_right\"><button type=\"button\" class=\"btn btn-info\"><a href='booking.jsp?RoomID=\""+roomID.get(i)+"\"'>预订</a></button></span>");
                                    out.print(" </div>" + "</div>" + "<hr>");
                                }
                            }
                        }
                    } catch (SQLException | ClassNotFoundException e) {
                        System.out.println(e);
                    }
                }
            %>
        </div>
    </div>
    </div>
</div>
</body>
</html>
